<%@ page language="java" import="java.util.*" pageEncoding="utf-8" %>
<%@ include file="../../../attr.jsp" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/fmt" prefix="fmt" %>

<!doctype html>
<html>
<head>
    <meta http-equiv="X-UA-Compatible" content="IE=emulateIE7"/>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
    <meta charset="utf-8">
    <link rel="stylesheet" href="${ctx}/css/sapar.css"/>
    <link rel="stylesheet" type="text/css" href="${ctx}/css/common.css"/>
    <link rel="stylesheet" type="text/css" href="${ctx}/css/kkpager_blue.css"/>
    <link href="${ctx}/css/select2.min.css" rel="stylesheet" />


    <title>包模板信息</title>
    <style type="text/css">
        #left {
            float: left;
            width: 100%;
        }

        #saper-container {
            float: left;
            width: 100%;
            background: url(${ctx}/images/dian.png) repeat-y 63% 0;
        }

        #table_left {
            float: left;
            width: 62%;
            min-height: 300px
        }

        #right {
            float: right;
            width: 36%;
            min-height: 300px
        }

        .s1 {
            color: red;
            font-weight: 900;
            font-size: 13px;

        }

        th {
            text-align: center;
        }

        td {
            text-align: center;
        }
    </style>

</head>

<body>
<div id="left">
    <div id="saper-container">
        <div id="saper-hd"></div>
        <div id="saper-bd">
            <div class="subfiled clearfix">
                <h2>包模板信息</h2>
            </div>
            <div class="subfiled-content">
                <div class="search-box clearfix">

                    <div class="kv-item clearfix">
                        <label>选择科室：</label>
                        <div class="kv-item-content">
                            <select onchange="selectBytype()" id="dep">
                                <option value="">请选择</option>
                                <c:if test="${keshilist != null}">
                                    <c:forEach items="${keshilist}" var="pro">
                                        <option value="${pro.departmentId}"
                                                <c:if test="${pro.departmentId==packageDepartid}">selected</c:if>>${pro.departmentName}</option>
                                    </c:forEach>
                                </c:if>
                            </select>
                        </div>
                    </div>

                    <div class="kv-item clearfix">
                        <label>关键字：</label>
                        <div class="kv-item-content">
                            <input id="searchTxt"  type="text" name="searchTxt" value="${search_Txt}" class="search_text"
                                   placeholder="包名/包简码" >
                        </div>
                    </div>


                    <a href="javascript:;" class="sapar-btn sapar-btn-recom query-btn">查询</a>
                </div>


                <!--表格开始-->
                <div id="table_left" class="table">
                    <!--表格操作-->
                    <div class="table-operate ue-clear alla">
                        <a href="javascript:;" class="add" id="addPack">增加模板</a>
                        <a href="javascript:;" class="edit" id="modifyPack">修改模板</a>
                        <a href="javascript:;" class="del" id="delPack">删除模板</a>
                        <a href="javascript:;" class="edit" id="copyPack">复制模板</a>

                        <input style="width: 50px; height: 20px" readonly type="text" id="qixieNum" value=""/>
                        <%--<a href="javascript:;" class="" id="print"> 打印</a>--%>
                    </div>
                    <!--表格具体内容-->
                    <div class="table-box">
                        <table>
                            <thead>
                            <tr>
                                <th><input type="checkbox" id="checkall" name="checkall"/></th>
                                <%--<th>编号</th>--%>
                                <th>包名</th>
                                <th>部门</th>
                                <th>打印条码</th>
                                <th>包装类型</th>
                                <th>灭菌方式</th>
                            </tr>
                            </thead>
                            <tbody>
                            <c:if test="${shenqingList != null}">
                                <c:forEach items="${shenqingList}" var="ssb">
                                    <tr class="xiangxi" data="${ssb.packageId}">
                                        <td><input type="checkbox" name="key" value="${ssb.packageId}"/></td>
                                        <%--<td>${ssb.packageId}</td>--%>
                                        <td>${ssb.packageName}</td>
                                        <td>
                                            <c:if test="${ssb.packageDepartid == 17 }">厂家包</c:if>
                                            <c:if test="${ssb.packageDepartid == 16}">--</c:if>
                                            <c:if test="${ssb.packageDepartid != 16 && ssb.packageDepartid != 17 }">${ssb.tbDepartment.departmentName}</c:if>
                                        </td>
                                        <td>
                                            <c:if test="${ssb.packagePrint == 0}">不打印</c:if>
                                            <c:if test="${ssb.packagePrint == 1}">打印</c:if>
                                        </td>
                                        <td>${ssb.zsDabao.dabaoName}</td>
                                        <td>${dictMap[ssb.packageMiejunFangshi+0].dictName}</td>

                                    </tr>
                                </c:forEach>
                            </c:if>
                            </tbody>
                        </table>
                    </div>
                    <div id="saper-ft">
                        <div id="kkpager"></div>
                    </div>
                </div><!--表格结束-->
                <div id="right">
                    <!--表格开始-->
                    <div class="table">
                        <div class="table-box">
                            <div class="table-operate ue-clear alla">
                                <a href="javascript:;" class="add" id="addQx">增加器械</a>
                                <a href="javascript:;" class="edit" id="modifyQx">修改器械</a>
                                <a href="javascript:;" class="del" id="delQx">删除器械</a>
                            </div>
                            <table id="detail">
                                <thead>
                                <tr>
                                    <th><input type="checkbox" name="keytwo" value="" id="checkalltwo"/></th>
                                    <th>器械名称</th>
                                    <th>器械别名</th>
                                    <th>器械尺寸</th>
                                    <th>器械规格</th>
                                    <th>器械数量</th>
                                </tr>
                                </thead>
                                <tbody>

                                </tbody>
                            </table>
                        </div>
                        <div>
                            <input id="leftPage" class="butLeft" type="button" value="上一页"/>
                            <input id="rightPage" class="butRight" type="button" value="下一页"/>
                            <span>到第&nbsp;</span><input id="goPage" class="textBox" type="text" value="1"/><span
                                style="margin-left: 4px;">页&nbsp;</span>
                            <input id="sureBut" class="sure" type="button" value="确定"/>
                            <span>共</span><span class="totalPages" id="totalNum">0</span><span>&nbsp;页</span>
                        </div>
                    </div>
                </div>
            </div>
        </div>

    </div>

    <!--  -->

</div>

<script type="text/javascript" src="${ctx}/js/jquery.js"></script>
<script type="text/javascript" src="${ctx}/js/sapar.js"></script>
<script type="text/javascript" src="${ctx}/js/WdatePicker.js"></script>
<script type="text/javascript" src="${ctx}/assets/layer/layer.js"></script>
<script type="text/javascript" src="${ctx}/js/kkpager.min.js"></script>
<script src="/js/select2.js" type = "text/javascript" ></script>
<script src="/js/pinyin.js" type = "text/javascript" ></script>
<script type="text/javascript">
    var pageSize = 10, currentPage = 1, totalPage;
    var data = [];
    var packageId = 0;
    $(function () {

        var departmentId = $('select option:selected').val();
        //定义一个全局变量
        var g_packageId = "";
        var totalPage = ${total};
        var pageNo = getParameter('pageIndex');
        if (!pageNo) {
            pageNo = 1;
        }
        var departmentId = $('select option:selected').val();
        //生成分页
        //有些参数是可选的，比如lang，若不传有默认值
        kkpager.generPageHtml({
            pno: pageNo,
            //总页码
            total: totalPage,
            //链接前部
            hrefFormer: '${ctx}/shoushumuban/ssmubanList',
            //链接尾部
            hrefLatter: '.do',
            getLink: function (n) {
                return this.hrefFormer + this.hrefLatter + "?pageIndex=" + n + "&departmentId=" + departmentId + "&search_Txt=" + $(".search_text").val() ;
            }
        });
        $(".query-btn").on("click", function () {
            departmentId = $('select option:selected').val();
            debugger
            window.location.href = "${ctx}/shoushumuban/ssmubanList.do?departmentId=" + departmentId +"&search_Txt="+$(".search_text").val();
        });
        <%--删除  包--%>
        $("#delPack").click(function () {
            var chk_value = getSelRecord();
            if (chk_value.length > 0) {
                layer.confirm('你确定要删除么？', function () {
                    $.ajax({
                        type: "POST",
                        url: "${ctx}/shoushumuban/packageDel.do?id=" + chk_value.join(),
                        error: function (request) {
                            layer.alert("网络故障，请稍后再试!");
                        },
                        success: function (data) {
                            if (data.status == "true") {
                                layer.alert("删除成功", function () {
                                    window.location.reload();
                                    $('input[name="key"]').each(function () {
                                        $("input[name='key']").prop('checked', false);
                                    });
                                });
                            } else {
                                layer.msg(data.msg);
                            }
                        }
                    });
                });
            } else {
                layer.msg('请选择要删除的数据！');
            }
        });

        $('select').select2();

        $("#addPack").click(function () {
            //window.location.href="${ctx}/shoushumuban/findAllProductList.do";
            window.location.href = "${ctx}/shoushumuban/packageAdd.do";
        });


        $("#copyPack").click(function () {
            var chk_value = getSelRecord();
            if (chk_value.length == 1) {
                window.location.href = "${ctx}/shoushumuban/packageEdit.do?id=" + chk_value[0]+"&type="+1;
            } else {
                layer.msg("请选择并只能选择一条数据！");
            }
        });


        $("#modifyPack").click(function () {
            var chk_value = getSelRecord();
            if (chk_value.length == 1) {
                window.location.href = "${ctx}/shoushumuban/packageEdit.do?id=" + chk_value[0]+"&type="+0;
            } else {
                layer.msg("请选择并只能选择一条数据！");
            }
        });

        <%----%>
        $("#checkall").click(function () {
            if (this.checked) {
                $("input[name='key']").attr('checked', true)
            } else {
                $("input[name='key']").attr('checked', false)
            }
        });

        $("#checkalltwo").click(function () {
            if (this.checked) {
                $("input[name='keytwo']").attr('checked', true)
            } else {
                $("input[name='keytwo']").attr('checked', false)
            }
        });


        //除了表头（第一行）以外所有的行添加click事件.
        $("tr").slice(1).click(function () {
            var test = $(this).siblings();
            test.removeAttr("tag");
            test.find("input[type='checkbox']").removeAttr("checked");
            // 切换样式
            $(this).toggleClass("tr_active");
            // 找到checkbox对象
            var chks = $("input[type='checkbox']",this);
            var tag = $(this).attr("tag");
            if(tag=="selected"){
                // 之前已选中，设置为未选中
                $(this).attr("tag","");
                chks.prop("checked",false);
            }else{
                // 之前未选中，设置为选中
                $(this).attr("tag","selected");
                chks.prop("checked",true);
            }
        });



        // --点击数据前面的选择框后面出结果 --
        $(".xiangxi").click(function () {
            $(this).addClass('s1'); //为选中项添加高亮
            $(this).siblings().removeClass('s1');//去除其他项的高亮形式

            var liushuiId = $(this).attr("data");
            g_packageId = liushuiId;
            $.ajax({
                type: "POST",
                url: "${ctx}/shoushumuban/ssbDetailList.do?id=" + liushuiId,
                error: function (request) {
                    layer.alert("网络故障，请稍后再试!");
                },
                success: function (data) {
                    if (data.status == "true") {
                        var qixieNum = data.num;
                        $("#qixieNum").val(qixieNum);
                        appendTable(data);
                    } else {
                        layer.msg(data.msg);
                    }
                }
            });

        });


        $("#addQx").click(function () {
            var chk_value = getSelRecord();
            /* alert(chk_value); */
            if (chk_value.length == 1) {
                layer.open({
                    type: 2,
                    title: '器械明细',
                    maxmin: true,
                    moveOut: true,
                    shadeClose: true, //点击遮罩关闭层
                    area: ['700px', '450px'],
                    content: "${ctx}/shoushumuban/shoushuqixieDetailList.do?gpackid=" + chk_value[0]
                });
            } else {
                layer.alert('请勾选且只能勾选一个包！');
            }
        });

        $("#modifyQx").click(function () {
            var chk_value = getSelRecordTwo();
            var packageid = $(".package").attr("data");
            if (chk_value.length == 1) {
                layer.open({
                    type: 2,
                    title: '',
                    maxmin: true,
                    moveOut: true,
                    shadeClose: true, //点击遮罩关闭层
                    area: ['700px', '450px'],
                    content: "${ctx}/shoushumuban/shoushuqixieUpdate.do?packageid=" + packageid + "&id=" + chk_value[0]
                });
            } else {
                layer.alert('请勾选且只能勾选一个器械修改！');

            }
        });


        $("#sureBut").click(function () {
            goPage();
        });
        $("#leftPage").click(function () {
            leftPages();
        });
        $("#rightPage").click(function () {
            rightPages();
        });

        $(".package").live("dblclick", function () {
            var img = $(this).attr("data2");
            layer.open({
                type: 1,
                title: false,
                closeBtn: true,
                shadeClose: true,
                area: '620px',
                offset: '100px',
                skin: 'layui-layer-nobg',
                content: '<img src="${ctx}' + img + '"/>'
            });
        });
        0

        <%--右边小表点击删除器械的事件--%>
        $("#delQx").click(function () {
            var packageid = $(".package").attr("data");
            console.log(packageid);
            var chk_value = getSelRecordTwo();//获取选中的
            if (chk_value.length > 0) {
                layer.confirm('你确定要删除么？', function () {
                    $.ajax({
                        type: "POST",
                        url: "${ctx}/shoushumuban/packageQixieDel.do?packageid=" + packageid + "&id=" + chk_value.join(),
                        error: function (request) {
                            layer.alert("网络故障，请稍后再试!");
                        },
                        success: function (data) {
                            if (data.status == "true") {
                                layer.alert("删除成功", function (index) {
                                    $.ajax({
                                        type: "POST",
                                        url: "${ctx}/shoushumuban/ssbDetailList.do?id=" + g_packageId,
                                        error: function (request) {
                                            layer.alert("网络故障，请稍后再试!");
                                        },
                                        success: function (data) {
                                            layer.close(index);
                                            if (data.status == "true") {
                                                appendTable(data);
                                            } else {
                                                layer.msg(data.msg);
                                            }
                                        }
                                    });
                                });

                            } else {
                                layer.msg(data.msg);
                            }
                        }
                    });
                });
            } else {
                layer.msg('请选择要删除的数据！');
            }
            /* alert(chk_value); */

        });
        <%--end--%>

    });
    
    function selectBytype() {
        $(".query-btn").click();
        
    }

    $(document).keydown(function (event) {

        var packageName = $("input[name='packageName']").val();
        if (event.keyCode == 13) {
            if ($("#searchTxt").is(":focus") ) {
                selectBytype();
            }
        }
    });

    function getSelRecord() {
        var chk_value = [];
        $('input[name="key"]:checked').each(function () {
            chk_value.push($(this).val());
        });
        return chk_value;
    }
    function getSelRecordTwo() {
        var chk_value = [];
        $('input[name="keytwo"]:checked').each(function () {
            chk_value.push($(this).val());
        });
        return chk_value;
    }

    function getshuju() {
        var liushuimingxi = [];
        $('input[name="key"]:checked').each(function () {
            var flag = $(this).attr("shuju");
            liushuimingxi.push($(this).attr("shuju"));

        });
        return liushuimingxi;
    }

    function getParameter(name) {
        var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)");
        var r = window.location.search.substr(1).match(reg);
        if (r != null) return unescape(r[2]);
        return null;
    }


    //重新执行点击出详细信息的方法
    function callbackData(liushuiId) {
        console.log(liushuiId);
        $.ajax({
            type: "POST",
            url: "${ctx}/shoushumuban/ssbDetailList.do?id=" + liushuiId,
            error: function (request) {
                layer.alert("网络故障，请稍后再试!");
            },
            success: function (data) {
                if (data.status == "true") {
                    appendTable(data);
                } else {
                    layer.msg(data.msg);
                }
            }
        });
    }
    //接受回调函数 寻找map
    function callbackData2(data) {
        var $tr = $("#" + data.productId);
        $tr.find("td").eq(3).text(data.num);
    }

    //=================================================================

    function appendTable(dataList) {
        currentPage = 1;
        data = dataList.mxList;
        packageId = dataList.packageId;
        creatTab();
    }

    //获得总页数
    function totalPages() {
        //如果刚好除尽
        if (data.length % pageSize == 0) {
            totalPage = data.length / pageSize;
        }
        else {
            totalPage = parseInt(data.length / pageSize) + 1;
        }
    }

    //上一页
    function leftPages() {
        if (currentPage != 1) {
            currentPage--;
            //开始创建
            creatTab();
        }
    }
    //下一页
    function rightPages() {
        if (currentPage != totalPage) {
            currentPage++;
            //开始创建
            creatTab();
        }
    }
    //到第几页去
    function goPage() {
        currentPage = document.getElementById('goPage').value;
        //如果输入的页数超出总页数，则到最后一页去！
        if (currentPage > totalPage) {
            currentPage = totalPage;
        }
        if (currentPage < 1) {
            currentPage = 1;
        }
        creatTab();
    }

    function creatTab() {
        debugger
        //清空数据
        $("#detail tbody").empty();
        $("#checkalltwo").attr("checked", false);
        //总页数
        totalPages();
        $('#totalNum').text(totalPage);
        //插入页数
        $('#goPage').val(currentPage);
        var str = '';
        $("#detail tbody").empty();
        currentRows = (totalPage == 1 || currentPage == totalPage) ? data.length : currentPage * pageSize;
        console.log(data);

        for (var i = (currentPage - 1) * pageSize; i < currentRows; i++) {
            var product = data[i].product;

            if(product == null ){
                str += "<tr class='package' id='" + data[i].productId + "'data='" + packageId + "' data2=''><td><input type='checkbox' name='keytwo' value='" + data[i].productId + "'/></td><td>" + data[i].productName + "</td><td>无</td><td></td><td></td><td>" + data[i].num + "</td></tr>";

            }else{
                str += "<tr class='package' id='" + data[i].productId + "'data='" + packageId + "' data2='" + data[i].product.productPic + "'><td><input type='checkbox' name='keytwo' value='" + data[i].productId + "'/></td><td>" + data[i].productName + "</td><td>"+data[i].product.productMark+"</td><td>" + data[i].product.productSize  + "</td><td>"+data[i].product.productBarcode+"</td><td>" + data[i].num + "</td></tr>";

            }

        }
        $("#detail tbody").append(str);
    }
</script>
</body>
</html>